<template>
  <div class="auto">
    <validator name='v'>
      <form class="form-horizontal">
        <div class="row" style="display: flex;justify-content: center;" v-if="billshow" id="testbill">
          {{{ bill.data}}}
        </div>
        <div class="row" style="display: flex;justify-content: center;" v-if="financebillshow" id="financebill">
          {{{financebill.data}}}
        </div>
        <div class="row"  style="margin-top: 10px">
          <div class="col-sm-2" style="margin-left: 10%" v-if="!messShow">
            <span><strong>发票号:{{model.f_bill_id}}</strong></span>
          </div>
          <div class="col-xs-offset-1" v-if="messShow">
            <span style="color: red"><strong>未获取到收据代码，发票不能正常打印，请确认后补打发票</strong></span>
          </div>
        </div>
        <div class="row" style="margin-top: 10px">
          <label for="f_rprint_type" class="col-sm-2 control-label">补打类型:</label>
          <div class="col-sm-2" >
            <v-select
            v-model="model.f_rprint_type"
            placeholder='请选择'
            :value.sync="model.f_rprint_type"
            :options='rprinttype'
            :disabled='true'
            close-on-select clear-button>
            </v-select>
          </div>
          <label for="f_rbill_type" class="col-sm-2 control-label" v-if="data.f_bill_style === '专用收据'">票据类型:</label>
          <div class="col-sm-2" v-if="data.f_bill_style === '专用收据'">
            <v-select
            v-model="model.f_rbill_type"
            placeholder='票据类型'
            :value.sync="model.f_rbill_type"
            :options='rbilltype'
            close-on-select clear-button>
            </v-select>
          </div>
        </div>
        <div class="row" style="margin-top: 10px">
          <div  :class="[$v.f_cause.required ? 'has-error' : 'has-success']">
            <label for="f_cause" class=" col-sm-2 control-label">补打原因:</label>
            <div class="col-sm-7">
              <textarea class="form-control" v-model="model.f_cause"
              v-validate:f_cause='{required: true }' rows="3" placeholder="补打原因"></textarea>
            </div>
          </div>
        </div>
      </form>
      <div style="text-align:right;height:auto;margin-top:30px;margin-right:50px;">
        <report-print id='testbill' v-if="billshow" top='25mm' left='60mm' width='100%' height='80%' style='td,th{font-size: 10pt!important;}'
         :notrepeat="true" v-if= 'bill.data && model.f_bill_id'></report-print>
         <report-print id='financebill' v-if="financebillshow" top='5mm' left='60mm' width='100%' height='80%' style='td,th{font-size: 10pt!important;}'
          :notrepeat="true" v-if= 'bill.data && model.f_bill_id'></report-print>
        <button class="btn btn-default" @click="clean()">取消</button>
      </div>
    </validator>
  </div>
</template>
<script>
/**
*票据补打
*/
import * as Util from '../../../Util'
import { DataModel } from 'vue-client'
export default {
  title: '票据补打',
  data () {
    return {
      id: '',
      bill: new DataModel('rs/report/sellgas_bill'),
      financebill: new DataModel('rs/report/sellonly_bill'),
      // returnsbill: new DataModel('rs/report/returns_bill'),
      model: {
        f_bill_id: '',
        f_cause: '',
        f_rprint_type: ['新票号补打'],
        f_rbill_type: ['专用收据']
      },
      f_billold_id: '',
      messShow: false,
      rprinttype: [{label: '新票号补打', value: '新票号补打'}],
      rbilltype: [{label: '国税发票', value: '国税发票'}, {label: '专用收据', value: '专用收据'}],
      financebillshow: false,
      returnsbillshow: false,
      billshow: false
    }
  },
  props: ['data'],
  watch: {
    'data' (val) {
      this.id = val.id
      this.model.f_rbill_type = [this.data.f_bill_style]
      if (val) {
        if (this.data.f_bill_type === '机表收费') {
          if (this.data.f_bill_style === '专用收据') {
            this.financebillshow = true
            this.billshow = false
            this.returnsbillshow = false
          } else {
            this.financebillshow = false
            this.billshow = true
            this.returnsbillshow = false
          }
        } else if (this.data.f_bill_style === '用户退费') {
          this.financebillshow = false
          this.billshow = false
          this.returnsbillshow = true
        }
      }
    },
    'model.f_rbill_type' (val) {
      if (val[0] === '国税发票') {
        this.bill.search(this.id)
        this.financebillshow = false
        this.billshow = true
        this.returnsbillshow = false
        this.getNewBillNumber(val[0])
      } else if (val[0] === '专用收据') {
        this.getNewBillNumber(this.data.f_bill_style)
        this.getBill()
      }
    }
  },
  ready () {
    this.data.f_bill_style = '专用收据'
    this.model.f_rbill_type = [this.data.f_bill_style]
    this.getNewBillNumber(this.data.f_bill_style)
    this.getBill()
  },
  methods: {
    getBill () {
      this.id = this.data.id
      if (this.data.f_bill_style === '专用收据') {
        this.financebill.search(this.id)
        this.financebillshow = true
        this.billshow = false
      } else if (this.data.f_bill_style !== '专用收据') {
        this.bill.search(this.id)
        this.financebillshow = false
        this.billshow = true
      }
    },
    getNewBillNumber (type) {
      let tablename = 't_billcollec'
      let condition = 'f_user = ' + "'" + Util.f.name + "'" + ' and f_bill_state = ' + "'" + '启用' + "'" + ' and f_bill_type = ' + "'" + type + "'"
      this.$SqlService.singleTable(tablename, condition).then((res) => {
        if (res.data[0]. f_bill_id > res.data[0].f_end_number) {
          res.data[0]. f_bill_state = '停用'
          this.$BillService.stopBill(res.data[0])
          this.messShow = true
        } else {
          this.model.f_bill_id = res.data[0].f_bill_id
          this.model.f_billcoll_id = res.data[0].id
          if (!res.data[0].f_bill_id) {
            this.messShow = true
          }
        }
      }).catch((error) => {
        this.messShow = true
      })
    },
    clean () {
      this.$info('取消操作')
      this.$dispatch('clean', '票据补打', this.row)
    }
  },
  events: {
    'print-success' () {
      if (this.model && this.data) {
        // if (this.model.f_rprint_type[0] === '新票号补打') {
        this.$PrintService.ReissueBill(this.model, this.data, 'new').then((res) => {
          this.$dispatch('success', '票据补打', this.data, res)
        }).catch((error) => {
          this.$dispatch('error', '票据补打', this.data, error)
        })
        // } else if (this.model.f_rprint_type[0] === '原票号补打') {
        //   this.$PrintService.ReissueBill(this.model, this.data, 'old').then((res) => {
        //     this.$dispatch('success', '票据补打', this.data, res)
        //   }).catch((error) => {
        //     this.$dispatch('error', '票据补打', this.data, error)
        //   })
        // }
      }
    },
    'print-error' () {
      this.$showAlert('打印失败，若有需要，请补打发票', 'error', 3)
      this.$dispatch('error', '票据补打', this.data, null)
    }
  }
}
</script>

<style>
</style>
